﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows8Accelerator.Core.Xaml.Helpers;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    public class InternalGridView : GridView, ISuperGroupStyleSelectorOwner
    {
        public InternalGridView()
        {
            GroupStyleSelector = new SuperGroupStyleSelector(this);            
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            var parent = this.GetParentOf<SuperListView>();
            if (parent != null)
            {
                var columnAndRowSpan = parent.GetItemColumnAndRowSpanSelector(item);
                element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, columnAndRowSpan.Item1);
                element.SetValue(VariableSizedWrapGrid.RowSpanProperty, columnAndRowSpan.Item2);
            }
            
            base.PrepareContainerForItemOverride(element, item);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (DefaultGroupStyle != null)
            {
                var superListView = this.GetParentOf<SuperListView>();
                if (DefaultGroupStyle.Panel != superListView.DynamicGroupedItemsPanelTemplate)
                    DefaultGroupStyle.Panel = superListView.DynamicGroupedItemsPanelTemplate;
                superListView.PropertyChanged += superListView_PropertyChanged;
            }
        }

        void superListView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var superListView = (SuperListView)sender;
            if (e.PropertyName.Equals("DynamicGroupedItemsPanelTemplate"))
            {
                if (DefaultGroupStyle.Panel != superListView.DynamicGroupedItemsPanelTemplate)
                    DefaultGroupStyle.Panel = superListView.DynamicGroupedItemsPanelTemplate;
            }
        }

        public SuperGroupStyle DefaultGroupStyle
        {
            get { return (SuperGroupStyle)GetValue(DefaultGroupStyleProperty); }
            set 
            { 
                SetValue(DefaultGroupStyleProperty, value); 
            }
        }
        public static readonly DependencyProperty DefaultGroupStyleProperty =
            DependencyProperty.Register("DefaultGroupStyle", typeof(SuperGroupStyle), typeof(InternalGridView), new PropertyMetadata(null));


        public DataTemplate GroupHeaderTemplate
        {
            get { return (DataTemplate)GetValue(GroupHeaderTemplateProperty); }
            set { SetValue(GroupHeaderTemplateProperty, value); }
        }
        public static readonly DependencyProperty GroupHeaderTemplateProperty =
            DependencyProperty.Register("GroupHeaderTemplate", typeof(DataTemplate), typeof(InternalGridView), new PropertyMetadata(null));

       

    }
}
